package com.ler.yrmall.product.config;

import com.ler.yrmall.basic.Result;
import com.ler.yrmall.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Optional;

/**
 * @author Leron
 * @version 1.0
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public Result<?> businessExceptionHandler(BusinessException ex) {
        log.error("系统异常",ex);
        Integer code = Optional.ofNullable(ex.getCode()).orElse(500);
        return Result.error(code,ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result<?> exceptionHandler(Exception ex) {
        log.error("系统异常",ex);
        return Result.error(ex.getMessage());
    }

}
